package com.wjd.gulimall.product.controller;

import com.wjd.common.utils.PageUtils;
import com.wjd.common.utils.R;
import com.wjd.gulimall.product.entity.CategoryEntity;
import com.wjd.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品三级分类
 *
 * @author weijiaduo
 * @email jiaduo.wei@gmail.com
 * @date 2025-05-24 16:46:00
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 树结构列表
     */
    @RequestMapping("/list/tree")
    // @RequiresPermissions("product:category:list")
    public R listTree(@RequestParam Map<String, Object> params) {
        List<CategoryEntity> tree = categoryService.listTree(params);
        return R.ok().put("data", tree);
    }

    /**
     * 分页列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);
        return R.ok().put("data", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    // @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCategory(category);
        return R.ok();
    }

    /**
     * 修改排序
     */
    @RequestMapping("/update/sort")
    // @RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity[] categories) {
        List<CategoryEntity> categoryList = Arrays.stream(categories)
                .filter(category -> category.getCatId() != null)
                .map(category -> {
                    CategoryEntity newCategory = new CategoryEntity();
                    newCategory.setCatId(category.getCatId());
                    newCategory.setParentCid(category.getParentCid());
                    newCategory.setSort(category.getSort());
                    return newCategory;
                }).collect(Collectors.toList());
        categoryService.updateBatchById(categoryList);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeMenusByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
